/*
 * CommandsFactory.cpp
 *
 * @author Bematech S.A.
 */

#include "bilbao/protocol/CommandsFactory.h"

namespace bilbao
{
	namespace protocol
	{
		CommandsFactory::CommandsFactory( void )
		{
			// Do nothing
		}

		CommandsFactory::~CommandsFactory( void )
		{
			// Do nothing
		}
		//--------------------------------------------------------------------------------
		ICommand* CommandsFactory::newInstance( const Vector< String >& buffer )
		{
			ICommand* comm = NULL;

			if( buffer.empty() ) return NULL;

			try
			{
				const size_t theSize = buffer.size();
				const String& funcao = buffer[0];

				std::cout << "CMD = " << funcao << std::endl;

				if ( funcao == "AssociarAssinatura" && (theSize >2) )
				{
					//static serial::command::AssociarAssinatura assass;
					//assass = serial::command::AssociarAssinatura( buffer[1], buffer[2] );
					//return assass;

					comm = new serial::command::AssociarAssinatura( buffer[1], buffer[2] );
				}
				else if ( funcao == "AtivarSAT" && (theSize >3) )
				comm = new serial::command::AtivarSAT( buffer[1], buffer[2], buffer[3],
						buffer[4] );
				else if ( funcao == "AtualizarSoftwareSAT" && (theSize >1) )
				comm = new serial::command::AtualizarSoftwareSAT( buffer[1] );
				else if ( funcao == "BloquearSAT" && (theSize >1) )
				comm = new serial::command::BloquearSAT( buffer[1] );
				else if ( funcao == "CancelarUltimaVenda" && (theSize >3) )
				comm = new serial::command::CancelarUltimaVenda( buffer[1], buffer[2],
						buffer[3] );
				else if ( funcao == "ComunicarCertificadoICPBRASIL" && (theSize >2) )
				comm = new serial::command::ComunicarCertificadoICPBRASIL( buffer[1],
						buffer[2] );
				else if ( funcao == "ConfigurarInterfaceDeRede" && (theSize >2) )
				comm = new serial::command::ConfigurarInterfaceDeRede( buffer[1], buffer[2] );
				else if ( funcao == "ConsultarNumeroSessao" && (theSize >1) )
				comm = new serial::command::ConsultarNumeroSessao( buffer[1] );
				else if ( funcao == "ConsultarSAT" )
				comm = new serial::command::ConsultarSAT();
				else if ( funcao == "ConsultarStatusOperacional" && (theSize >1) )
				comm = new serial::command::ConsultarStatusOperacional( buffer[1] );
				else if ( funcao == "DesbloquearSAT" && (theSize >1) )
				comm = new serial::command::DesbloquearSAT( buffer[1] );
				else if ( funcao == "EnviarDadosVenda" && (theSize >3) )
				comm = new serial::command::EnviarDadosVenda( buffer[1], buffer[2],
						buffer[3] );
				else if ( funcao == "ExtrairLogs" && (theSize >1) )
				comm = new serial::command::ExtrairLogs( buffer[1] );
				else if ( funcao == "TesteFimAFim" && (theSize >3) )
				comm = new serial::command::TesteFimAFim( buffer[1], buffer[2], buffer[3] );
				else if ( funcao == "TrocarCodigoDeAtivacao" && (theSize >4) )
				comm = new serial::command::TrocarCodigoDeAtivacao( buffer[1], buffer[2],
						buffer[3], buffer[4] );
			}
			catch (...)
			{
				//TODO: definir qual execeção vai rolar
				LOG_E("Erro inesperado")
				return NULL;

			}

			return comm;
		}
	} // namespace protocol
} // namespace bilbao
